package Thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

//实现一个固定线程个数的线程池
class Mythreadpool{
    private BlockingQueue<Runnable> queue = null;
    public Mythreadpool(int n) {
        //初始化线程池，创建固定个数的线程
         queue=new ArrayBlockingQueue<>(1000);
        //创建n个线程
        for (int i = 0; i < n; i++) {
            Thread t=new Thread(()->{
                   try {
                       while (true) {
                           Runnable task = queue.take();
                           task.run();
                       }
                   } catch (InterruptedException e) {
                       throw new RuntimeException(e);
                   }
            });
            t.start();
        }

    }
    public void submit(Runnable task) throws InterruptedException {
        //把任务放到队列里面去
        queue.put(task);
    }

}
public class Dom33 {
    public static void main(String[] args) throws InterruptedException {
        Mythreadpool pool=new Mythreadpool(10);
        //向线程提交任务
        for (int i = 0; i <100 ; i++) {
            int id=i;
            pool.submit(()->{
                System.out.println(Thread.currentThread().getName() + " id "+id);
            });
        }
        //
    }
}
